/*
 * @Author: Jason Liu
 * @Date: 2022-03-10 10:38:11
 * @Desc: 
 */
import Vue from "vue";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

//路由注册
import { microRouter } from "w-vue-middle/microService";

import { system } from "./router.config";

import { nextPage } from "w-vue-middle/utils/auth"; //鉴权

const noPage = require("w-vue-middle/components/403").default;

let routes = [
  {
    path: "/",
    redirect: "/modelHDW"
  },
  {
    path: "/login",
    name: "login",
    hideInMenu: true,
    component: resolve => require(["w-vue-basis/login"], resolve)
  },
  {
    path: "/403",
    name: "403",
    hideInMenu: true,
    component: noPage
  },
  {
    path: "*",
    name: "404",
    hideInMenu: true,
    component: resolve => require(["w-vue-middle/components/404"], resolve)
  },
  ...system
];

const router = new microRouter({
  scrollBehavior: () => ({ y: 0 }),
  mode: "history",
  base: `${process.env.BASE_URL}`,
  routes: routes
});

router.beforeEach((to, from, next) => {
  if (to.path !== from.path) {
    NProgress.start();
  }

  if (to.meta.whitelist || to.name == "login") {
    next();
  } else {
    nextPage(to, from, next);
  }

  NProgress.done();
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
